package org.hrqing.chess;

import it.sauronsoftware.junique.AlreadyLockedException;
import it.sauronsoftware.junique.JUnique;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hrqing.chess.config.props.SystemConfigProps;
import org.hrqing.chess.frame.MainFrame;
import org.hrqing.chess.service.GameService;
import org.hrqing.chess.service.LogService;
import org.hrqing.chess.service.PikaFishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import javax.swing.*;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@SpringBootApplication
public class Launcher implements CommandLineRunner {

    static {
        System.setProperty("java.awt.headless",  "false");
    }

    public static MainFrame frame;

    @Resource
    private ApplicationContext context;

    public static void main(String[] args) {
        AtomicReference<String> applicationName = new AtomicReference<>();
        try {
            // 此处为应用程序启动逻辑
            SpringApplication application = new SpringApplication(Launcher.class);
            application.setWebApplicationType(WebApplicationType.NONE);
            ConfigurableApplicationContext applicationContext = application.run(args);
            applicationName.set(applicationContext.getEnvironment().getProperty("spring.application.name"));
            log.info("application name {}", applicationName.get());
            JUnique.acquireLock(applicationName.get());
        } catch (AlreadyLockedException e) {
            log.error("application already locked", e);
            System.exit(1);
        }
        // 当应用程序退出时释放锁
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            log.info("application release locked");
            JUnique.releaseLock(applicationName.get());
        }));
    }

    @Override
    public void run(String... args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                SystemConfigProps systemConfigProps = context.getBean(SystemConfigProps.class);
                log.info("load system config");
                LogService logService = context.getBean(LogService.class);
                GameService gameService = context.getBean(GameService.class);
                PikaFishService pikaFishService = context.getBean(PikaFishService.class);
                frame = new MainFrame(systemConfigProps, logService, gameService, pikaFishService);
                log.info("create MainFrame");
                frame.afterPropertiesSet();
                frame.setVisible(true);
            }
        });
    }
}
